{extend name="layout/tpl" /}

{block name="title"}授权{/block}

{block name="css"}
{__block__}
<link rel="stylesheet" href="/backend/css/zTreeStyle.css">
<style type="text/css">

</style>
{/block}
{block name="content"}
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>节点列表</legend>
</fieldset>
<form class="layui-form">
    <ul id="ztree" class="ztree"></ul>
    <br><br>
    <div class="layui-form-item">
        <div class="layui-input-inline">
            <input type="hidden" name="gid" value="{$gid}">
            <button class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
        </div>
    </div>
</form>
{/block}

{block name="footerjs"}
<script type="text/javascript" src="/backend/js/jquery.ztree.core.min.js"></script>
<script type="text/javascript" src="/backend/js/jquery.ztree.excheck.min.js"></script>
<script>
    var setting = {
        check: {
            enable: true,
            showIcon: false,
            chkDisabledInherit: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onNodeCreated: onNodeCreated
        }
    };
    function onNodeCreated(event, treeId, treeNode) {
        if(treeNode.pId && !treeNode.isParent) {
            $('#' + treeNode.tId).css('display', 'inline');
        }
    }
    var rules = {php}echo $rules;{/php};
    var zNodes = rules;
    layui.use(['layer', 'form'], function() {
        var layer = layui.layer,
            $ = layui.jquery,
            form = layui.form;
        $.fn.zTree.init($("#ztree"), setting, zNodes);
        form.on('submit(submit)',function(data){
            var index;
            var pindex = parent.layer.getFrameIndex(window.name);
            var treeObj = $.fn.zTree.getZTreeObj("ztree"),
                nodes = treeObj.getCheckedNodes(true),
                rules = "";
            for(var i=0;i<nodes.length;i++){
                rules += nodes[i].id + ",";
            }
            rules = rules.substring(0, rules.length-1);
            $.ajax({
                type:       'post',
                data:       {gid:$(".gid").val(), rules:rules},
                dataType:   'json',
                beforeSend: function () {
                    index = layer.load(0, {shade: false});
                },
                success:    function (msg) {
                    layer.close(index);
                    if(msg.errCode === "0") {
                        parent.layer.close(pindex);
//                        parent.location.reload();
                    } else {
                        layer.msg(msg.msg);
                    }
                }
            });
        });
    });
</script>
{/block}